package com.zhongyu.item.consumer.elastic;

import com.zhongyu.comm.api.CinemaApi;
import com.zhongyu.comm.api.ImageApi;
import com.zhongyu.comm.api.ItemApi;
import com.zhongyu.comm.pojo.Cinema;
import com.zhongyu.comm.pojo.Image;
import com.zhongyu.comm.pojo.Item;
import com.zhongyu.comm.util.Constants;
import com.zhongyu.comm.util.DateUtis;
import org.springframework.beans.BeanUtils;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/*
 *同步dm_item表的内容到ES
 */
@Component
public class CreateIndexAndSyncData {

    @Resource
    private ItemApi itemApi;
    @Resource
    private ImageApi imageApi;
    @Resource(name = "myRedisTemplate")
    private RedisTemplate redisTemplate;
    @Resource
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Resource
    private CinemaApi cinemaApi;

    @Scheduled(initialDelay = 3000, fixedDelay = 100000)
    public void transferDataToEs() {
        ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
        LocalDateTime syncTime = (LocalDateTime) valueOperations.get("sync:time");
        System.out.println(syncTime);
        List<Item> list = null;
        if (syncTime == null) {
            boolean flag = elasticsearchRestTemplate.indexExists("item-index");
            if (flag == true) {
                elasticsearchRestTemplate.deleteIndex("item-index");
            }
            elasticsearchRestTemplate.createIndex(ItemDoc.class);
            elasticsearchRestTemplate.putMapping(ItemDoc.class);
            list = itemApi.findAll();
        } else {
            list = itemApi.findBySyncTime(syncTime);
        }
        valueOperations.set("sync:time", LocalDateTime.now());

        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                // if (i>0&&i%1000==0){
                //  List<Item>children=list.subList(i-1000,i);
                Item item = list.get(i);
                //如果数据量超大,可以把children传入rabbitMQ，通过异步方式执行下面的代码
                //for (Item item:children){
                ItemDoc itemDoc = new ItemDoc();
                BeanUtils.copyProperties(item, itemDoc);
                Image image = imageApi.findOne(item.getId(), Constants.Image.ImageType.normal, Constants.Image.ImageCategory.item);
                Cinema cinema = cinemaApi.findById(item.getCinemaId());
                itemDoc.setAreaId(cinema.getAreaId());
                itemDoc.setAreaName(cinema.getAreaName());
                itemDoc.setAddress(cinema.getAddress());
                itemDoc.setItemTypeId1(item.getItemType1Id());
                itemDoc.setItemTypeId2(item.getItemType2Id());
                itemDoc.setCreatedTime(item.getCreatedTime().toLocalDate());
                itemDoc.setStartTime(item.getStartTime().toLocalDate());
                itemDoc.setEndTime(item.getEndTime().toLocalDate());
                if (image != null) {
                    itemDoc.setImgUrl(image.getImgUrl());
                } else {
                    itemDoc.setImgUrl("https://img.alicdn.com/bao/uploaded/i3/2251059038/O1CN01MIfXXU2GdSFhxP5x5_!!0-item_pic.jpg");
                }
                elasticsearchRestTemplate.save(itemDoc);
            }
        }

    }
}

